
from sqlalchemy import create_engine
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

class Datas(object):
    def listall(self, df):
        for index, row in df.iterrows():
            print(row)

    def plot(self):
        x = np.array([1,2,3,4,5,6,7,8])
        y = np.array([3,5,7,6,2,6,10,15])
        plt.plot(x,y,'r')# 折线 1 x 2 y 3 color
        plt.plot(x,y,'g',lw=10)# 4 line w
        # 折线 饼状 柱状
        x = np.array([1,2,3,4,5,6,7,8])
        y = np.array([13,25,17,36,21,16,10,15])
        plt.bar(x,y,0.2,alpha=1,color='b')# 5 color 4 透明度 3 0.9
        plt.show()

    def readcsv(self, filename):
        with open(filename) as f:
            reader = csv.reader(f)
            for row in reader:
                print(row)

    def todatabase(self):
        engine = create_engine(
            'postgresql://rolename@localhost:5432/databasename')  # create_engine说明：dialect[+driver]://user:password@host/dbname[?key=value..]
        data = pd.DataFrame(np.arange(15).reshape(3, 5), index=['one', 'two', 'three'],
                            columns=['a', 'b', 'c', 'd', 'e'])
        try:
            data.to_sql('tablename', engine, index=False, if_exists='append')
        except Exception as e:
            print(e)
